CLOSING FILES IN KYAN PASCAL by Erik Warren, 18 March 1987_______________________________________To us Kyan Tech Support guys, it seems as though everybody and their motherasks us a derivative of this question: "In Kyan Pascal, how do I close a fileafter I am done with it?"The answer: You don't.The manner in which Kyan Pascal handlesthe closing of files is known as 'GoodHousekeeping' or 'Lazy I/O.' Insteadof requiring the programmer to issue aClose command, the compiler closes thefile when the program exits theprocedure or function in which the filewas declared and opened.So, if your program uses global files,they will not be closed until the final"END." of the program is encountered.If you are using a lot of files, youmay eventually encounter a DOS errorlike 'too many open files.' (That'llteach ya! You shouldn't be usingglobal files/variables, etc. anyway;you should think of modular coding withlocal files/variables.)With the following program...PROGRAM abc(Input,Output,Fyle); VAR Fyle : Text;BEGIN Reset(Fyle,'D:FILENAME.EXT'); . . .END....Fyle will NOT be closed until END.is reached. Here is how you shouldmake Fyle local...PROGRAM abc(Input,Output); PROCEDURE xyz; VAR Fyle : Text; BEGIN Reset(Fyle,'D:FILENAME.EXT'); . . . END; (* of procedure xyz *)BEGIN xyz; . . .END. (* of main program *)When xyz is called in the aboveprogram, Fyle will be opened and thenclosed when xyz is exited._______________________________________THE FURTHER ADVENTURES OF JOE PROGRAMMER_______________________________________Now let's suppose my good friend JoeProgrammer programs himself into ahopeless situation where he doesn'twant to exit a procedure to close afile. In addition to being the kind ofperson who paints himself into thecorner of a room, Joe also programshimself into corners. (For this reason and others, Joe Programmer is theworld's most frequent user of Pascal's Label and Goto statements.)Lucky for him, just as a fighter pilotcan eject from a burning plane, Joealso has a last-ditch 'Bail Button.'This unorthodox, back-door approach ofclosing files is done via CIO...FUNCTION Close(IOCB_Num : Integer) : Integer;BEGIN IOCB_Num := IOCB_Num * 16; Close := 0;#A STX _t ;for safety's sake LDY #7 ;sp offset to IOCB_Num LDA (_sp),y ;pull user's IOCB # TAX ;accum. to X reg. LDA #12 ;close command STA $342,x ;store in ICCMD (ICCOM) LDA #$00 ;zero out aux. bytes... STA $034A,x ;one... STA $034B,x ;and two JSR $E456 ;jump via CIO vector TYA ;Y reg. to accum. LDY #5 ;sp offset to Close LSB STA (_sp),y ;store CIO's error code LDX _t ;safety#END;This function is called like this: Status := Close(1);The integer you pass to it is the IOCBnumber you wish to Close. Since youdon't usually know which IOCB# thecompiler has allocated for your file(s)you may want to make a loop to closemultiple IOCB's. Make sure not toclose #0 or #6. The integer returnedis the CIO status byte; a '1' wouldindicate everything is okay.Remember, this is the "Wimp's Way Out."The preferable way to close a file isto exit the block of code it was openedin.